package hu.fic.weddingstorefinder.activity;

import hu.fic.weddingstorefinder.R;
import hu.fic.weddingstorefinder.db.WeddingStoreDBAdapter;
import hu.fic.weddingstorefinder.db.table.POI;
import hu.fic.weddingstorefinder.parcel.DressParcelable;
import hu.fic.weddingstorefinder.utils.Constants;
import hu.fic.weddingstorefinder.utils.Utils;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

public class DressDetailsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dressdetails_layout);
		final DressParcelable dressParcel = this.getIntent().getParcelableExtra(Constants.DRESS_PARCEL_KEY);
		try {
			final POI poi = new POI(WeddingStoreDBAdapter.getInstance().getDb(), this);
			poi.setCustomPOIId(dressParcel.getCustomPOIId());
			poi.load();
			this.setTitle(poi.getPOIName());
			SmartImageView dressImageView = (SmartImageView) this.findViewById(R.id.dressdetails_dress_imageView);
			String imageURLString = Constants.IMAGE_BASE_URL + dressParcel.getImageName();
			Log.i(Constants.LOG_TAG_APP, "image url: " + imageURLString);
			dressImageView.setImageUrl(imageURLString);
			
			TextView dressDescTV = (TextView)findViewById(R.id.dressdetails_dressdesc_label);
			dressDescTV.setText(dressParcel.getDressDesc());
			
			TextView priceValueTV = (TextView)findViewById(R.id.dressdetails_priceValue_label);
			priceValueTV.setText(Utils.formatCurrency(dressParcel.getPrice(), dressParcel.getCurrency()));
			
			TextView availableTV = (TextView)findViewById(R.id.dressdetails_isavailable_label);
			int textColor;
			String availableText;
			if (dressParcel.getIsAvailable() == Constants.BOOL_TRUE) {
				textColor = Color.GREEN;
				availableText = getString(R.string.available);
			} else {
				textColor = Color.RED;
				availableText = getString(R.string.not_available);
			}
			availableTV.setTextColor(textColor);
			availableTV.setText(availableText);
			
			//show map button config:
			Button showMapButton = (Button) findViewById(R.id.dressdetails_showmap_button);
			showMapButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					try {
						Intent myIntent = new Intent(DressDetailsActivity.this, StoreMapActivity.class);
						double[]storeCoordinates = {poi.getLongitude(), poi.getLatitude()};
						myIntent.putExtra(Constants.COORDINATES_TO_SHOW_KEY, storeCoordinates);
				        myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				        startActivity(myIntent);
					} catch (ActivityNotFoundException e) {
						e.printStackTrace();
					}
				}
			});
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
